﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMStudio.Common;
using AMStudio.DelegateDispather;
using AccessDB;

namespace AMStudio.UserInterface
{
    public partial class MaterialForm : AMStudio.Common.AWinFormShell
    {
        private string materialBinNr = string.Empty;
        public string MaterialBinNr
        {
            get { return materialBinNr; }
            set { materialBinNr = value; }
        }

        public MaterialForm()
        {
            InitializeComponent();
        }

        public override bool ActivateUI()
        {
            this.UIName = this.Name;
            base.ActivateUI();

            Language_InitializeControl(this);
            //db.InitProducts();
            //RefreshUI();
            if (this.Visible == false)
            {
                this.WindowState = FormWindowState.Normal;
                this.Hide();
                this.Show();
            }
            else
                this.Hide();

            this.TopLevel = true;
            //this.Show();
            return true;
        }

        protected override void Language_RfreshTag(LanguageItem item)
        {
            base.Language_RfreshTag(item);


            if (item.RelatedControl is Panel)
            {
                this.Text = item.Language[LanguageString].ToString();
                item.RelatedControl.Text = string.Empty;
            }

        }

        protected override void Language_InitializeControl(Control UIControl)
        {
            base.Language_InitializeControl(UIControl);


            if (UIControl is Panel)
            {


                Panel bar = UIControl as Panel;

                LanguageItem item = LanguageDB.UIDBTags[this.UIName + "." + bar.Name] as LanguageItem;

                if ((item != null) && (item.Language[LanguageString] != null))
                {
                    this.Text = item.Language[LanguageString].ToString();
                    UIControl.Text = string.Empty;
                }

            }

        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0112 && (int)m.WParam == 0xF060)
            {
                this.Hide();
                return;
            }
            if (m.Msg == 0x0112)
            {
                if (m.WParam.ToInt32() == 0xF020)
                {
                    this.Hide();
                    return;
                }
            }
            base.WndProc(ref m);
        }

        protected override void BackGroud_EventHandler(object sender, DelegateDispather.StationEventArgs e)
        {
            base.BackGroud_EventHandler(sender, e);
            switch (e.Command)
            {
                case "MATERIAL_QUANTITY":
                    {
                        object[] obj = (object[])e.Value;
                        this.textBoxX2.Text = obj[0].ToString();
                        this.textBoxX1.Text = obj[1].ToString();
                    }
                    break;
                case "CHECK_MATERIAL_ERROR":
                    {
                        System.Windows.Forms.MessageBox.Show("箱号不对！请重新扫描物料箱号！");
                        this.textBoxX7.Text = "";
                        textBoxX7.Focus();
                    }
                    break;
                case "DISPATCH_INFO":
                    {
                        object[] obj = (object [])e.Value;
                        if (obj[0].ToString() != string.Empty)
                            this.textBoxX5.Text = obj[0].ToString();
                        if (obj[1].ToString() != string.Empty)
                            this.textBoxX6.Text = obj[1].ToString();
                        if (obj[2].ToString() != string.Empty)
                        {
                            this.comboBox1.Text = obj[2].ToString();
                        }
                        if (textBoxX5.Text == textBoxX6.Text && textBoxX6.Text != "0")
                        {
                            MaterialForm form = new MaterialForm();
                            System.Threading.Thread thread = new System.Threading.Thread(form.ShowDispatch);
                            thread.Start();
                        }
                    }
                    break;
                case "SET_EXISTING_BOX_ERROR":
                    {
                        System.Windows.Forms.MessageBox.Show("箱号不对，请重新扫描！");
                        this.textBoxX4.Text = "";
                        this.textBoxX4.Focus();
                    }
                    break;
                case "CUSTOMER_SN":
                    {
                        textBoxX3.Text = e.Value.ToString();
                    }
                    break;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            SendMessage("checkmaterial", textBoxX7.Text);
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            SendMessage("DISPATCH", null);
            this.comboBox1.Text = "";
            this.textBoxX5.Text = "";
            this.textBoxX6.Text = "";
        }

        private void buttonX26_Click(object sender, EventArgs e)
        {
            //todo
            //print
            //SendMessage("REPRINT", null);
            SendMessage("DISPATCH_PRINT", comboBox1.Text);
        }

        private void buttonX25_Click(object sender, EventArgs e)
        {
            if (textBoxX5.Text != "" && textBoxX6.Text != textBoxX5.Text)
            {
                System.Windows.Forms.MessageBox.Show("前一箱未做完，此时不能拼箱！");
                textBoxX4.Text = "";
                return;
            }
            SendMessage("SET_EXISTING_BOX", this.textBoxX4.Text);
            this.textBoxX4.Text = "";
        }

        private void MaterialForm_Load(object sender, EventArgs e)
        {
            base.SubscribeEvent("MANAGER_RET");
        }

        private void MaterialForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
        }

        private void textBoxX7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (textBoxX7.Text == string.Empty || textBoxX7.Text.Length != 24)
                {
                    System.Windows.Forms.MessageBox.Show("箱号不对，请重试！");
                    return;
                }
                if (textBoxX2.Text.Length>0 && Convert.ToInt32(textBoxX2.Text) > 2)
                {
                    if (System.Windows.Forms.MessageBox.Show("物料还未用完，确定要激活新的箱号？", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        SendMessage("checkmaterial", textBoxX7.Text);
                    }
                }
                else
                {
                    SendMessage("checkmaterial", textBoxX7.Text);
                }
            }
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            DataTable dt = AccessHelper.dataTable("select top 20 * from DISPATCH ORDER BY ID DESC");
            if (dt.Rows.Count > 0)
            {
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember = "BOXNR";
            }
        }

        private void ShowDispatch()
        {
            System.Windows.Forms.MessageBox.Show("已满箱，请粘贴标签后确认！");
            SendMessage("DISPATCHED", null);
        }
    }
}
